package com.jhc.common.core.utils;

import com.github.pagehelper.PageHelper;
import com.jhc.common.core.context.SqlParam;
import com.jhc.common.core.text.Convert;
import com.jhc.common.core.utils.sql.SqlUtil;
import com.jhc.common.core.web.page.PageDomain;


/**
 * 分页工具类
 *
 * @author ruoyi
 */
public class PageUtils extends PageHelper {

    /**
     * 排序列
     */
    public static final String ORDER_BY_COLUMN = "orderByColumn";

    /**
     * 排序的方向 "desc" 或者 "asc".
     */
    public static final String IS_ASC = "isAsc";

    /**
     * 分页参数合理化
     */
    public static final String REASONABLE = "reasonable";


    /**
     * 设置请求分页数据
     */
    public static void startPage(PageDomain pageDomain) {
        String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
        SqlParam.setParam(pageDomain);
        PageHelper.startPage(pageDomain.getPageNum(), pageDomain.getPageSize(), orderBy)
                .setReasonable(true);
    }

    /**
     * 清理分页的线程变量
     */
    public static void clearPage() {
        PageHelper.clearPage();
        SqlParam.clear();
    }
}
